package org.cqut.system.common.controller;

import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.common.dto.WorkDto;
import org.cqut.system.common.service.CommonService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;

/**
 * @author XiAoXu
 * @date 2021/10/31 14:34
 * @description
 */
@RestController
@RequestMapping("/common")
public class CommonController {
    private final CommonService commonService;

    public CommonController(CommonService commonService) {
        this.commonService = commonService;
    }

    @PostMapping(path = "/email/{email}")
    public ResultInfo email(@PathVariable String email) {
        String validate = commonService.sendEmail(email);
        if ("".equals(validate) || validate == null) {
            return new ResultInfo(Status.EMAIL_SEND_ERROR.code, Status.EMAIL_SEND_ERROR.message);
        } else {
            return new ResultInfo(Status.EMAIL_SEND_SUCCESS.code, Status.EMAIL_SEND_SUCCESS.message, validate);
        }
    }

    @PostMapping(path = "/avatar")
    public ResultInfo avatar(MultipartFile file, String username) {
        String path = commonService.uploadAvatar(file, username);
        if ("".equals(path) || path == null) {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        } else {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message, path);
        }
    }

    @PostMapping("/resource")
    public ResultInfo resource(MultipartFile[] file) {
        boolean flag = commonService.uploadResource(file);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/work")
    public ResultInfo uploadWork(MultipartFile file
            , @RequestParam("date") String[] date
            , @RequestParam("courseId") Long courseId
            , @RequestParam("workName") String workName) {
        WorkDto workDto = new WorkDto();
        workDto.setDate(date);
        workDto.setCourseId(courseId);
        workDto.setWorkName(workName);
        boolean flag = commonService.uploadWork(file, workDto);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/submit")
    public ResultInfo submitWork(MultipartFile file, @RequestParam("recordId") String recordId) {
        boolean flag = commonService.submitWork(file, recordId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @GetMapping("/download/{resourceId}")
    public void download(HttpServletResponse response, @PathVariable String resourceId) {
        commonService.download(response, resourceId);
    }

    @GetMapping("/download/work/{recordId}")
    public void downloadWork(HttpServletResponse response, @PathVariable String recordId) {
        commonService.downloadWork(response, recordId);
    }

    @GetMapping("/download/submit/{submitId}")
    public void downloadSubmit(HttpServletResponse response, @PathVariable String submitId) {
        commonService.downloadSubmit(response, submitId);
    }

    @GetMapping("/download/submits/{pathId}")
    public void downloadSubmits(HttpServletResponse response, @PathVariable String pathId) {
        commonService.downloadSubmits(response, pathId);
    }

    @PostMapping("/pack")
    public String downloadSubmit(HttpServletResponse response, @RequestBody Map<Object, Object> map) {
        List<String> ids = (List<String>) map.get("ids");
        try {
            return commonService.pack(response, ids);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @GetMapping("/template")
    public void excelTemplate(HttpServletResponse response) {
        try {
            commonService.template(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}